package 剑指offer;

import java.util.*;

public class 剑指Offer50第一个只出现一次的字符 {
    //暴力法
    public static char firstUniqChar1(String s) {
        int length = s.length();
        char[] chars = s.toCharArray();
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                if (i == length - 1 && i == j){
                    return chars[i];
                }
                if (i == j){
                    continue;
                }
                if (chars[i] == chars[j]){
                    break;
                }
                if (j == length-1 && chars[i] != chars[j]){
                    return chars[i];
                }
            }
        }
        return ' ';
    }
    //hashMap
    public static char firstUniqChar2(String s) {
        HashMap<Character,Boolean> map = new HashMap<>();
        char[] chars = s.toCharArray();
        for(char c : chars){
            map.put(c,!map.containsKey(c));
        }
        for(char c : chars){
            if (map.get(c)) return c;
        }
        return ' ';
    }
    //hashMap方法二，第二次遍历Map
    public char firstUniqChar(String s) {
        Map<Character, Boolean> dic = new LinkedHashMap<>();
        char[] sc = s.toCharArray();
        for(char c : sc)
            dic.put(c, !dic.containsKey(c));
        for(Map.Entry<Character, Boolean> d : dic.entrySet()){
            if(d.getValue()) return d.getKey();
        }
        return ' ';
    }

}
